module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  this.gameChanel = this.channelService.getChannel("GameChannel",true)
}

var handler = Handler.prototype

//购买商品
handler.buyGoods = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var goodsId = msg.goodsId
	var count = msg.count
	if(typeof(count) !== "number" || count < 1){
		count = 1
	}
	this.app.rpc.db.shop.buyGoods(null,uid,goodsId,count,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//获取购买信息(月卡、礼包等)
handler.getMonthCardInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.shop.getMonthCardInfo(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//获取月卡奖励
handler.getMonthCardAward = function(msg,session,next) {
	var uid = session.get("uid")
	var monthCardType = msg.monthCardType
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.shop.getMonthCardAward(null,uid,monthCardType,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}


//获取礼包信息
handler.getGiftBagInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.shop.getGiftBagInfo(null,uid,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}